package com.kitty.game.zhenmolu.service;

import com.kitty.common.db.Db4CommonService;
import com.kitty.common.thread.IdGenerator;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.ServerService;
import com.kitty.game.bag.message.RespIconCartoon;
import com.kitty.game.base.service.BagService;
import com.kitty.game.config.NPC;
import com.kitty.game.enter.RespGeneralNotify;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.equip.model.EquipField;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.equip.service.EquipService;
import com.kitty.game.friend.service.FriendService;
import com.kitty.game.i18n.I18nId;
import com.kitty.game.identity.IdentityType;
import com.kitty.game.item.service.PetStoneService;
import com.kitty.game.market.MarketService;
import com.kitty.game.market.message.ResMarketRecords;
import com.kitty.game.market.message.RespMarketGoodCard;
import com.kitty.game.market.message.RespMarketPetCard;
import com.kitty.game.market.message.to.CardInfoParam;
import com.kitty.game.market.message.vo.MarketTransactionVo;
import com.kitty.game.market.model.Goods;
import com.kitty.game.market.model.MarketTransactionRecord;
import com.kitty.game.pet.message.RespPetHost;
import com.kitty.game.pet.model.Pet;
import com.kitty.game.pet.service.PetService;
import com.kitty.game.player.model.HunPoProp;
import com.kitty.game.role.model.Role;
import com.kitty.game.role.service.RoleService;
import com.kitty.game.team.message.RespMsg;
import com.kitty.game.utils.AsktaoUtil;
import com.kitty.game.utils.NotifyModules;
import com.kitty.game.utils.NumberUtil;
import com.kitty.game.utils.TimeUtil;
import com.kitty.game.zhenbao.message.*;
import com.kitty.game.zhenbao.message.vo.MoneyGoods;
import com.kitty.game.zhenbao.model.Zhenbao;
import com.kitty.game.zhenmolu.message.RespZhenMoLuBonus;
import com.kitty.game.zhenmolu.message.RespZhenMoLuData;
import com.kitty.game.zhenmolu.message.RespZhenMoLuResult;
import com.kitty.game.zhenmolu.model.ZhenMoLu;
import com.kitty.listener.EventDispatcher;
import com.kitty.listener.EventType;
import com.kitty.listener.event.RemovePetEvent;
import com.kitty.mina.cache.SessionUtils;
import com.kitty.mina.message.MessagePusher;
import com.kitty.mina.task.ThreadLocalUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.mina.core.session.IoSession;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.json.Json;
import org.nutz.json.JsonFormat;
import org.nutz.lang.Strings;
import org.nutz.lang.util.NutMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 镇魔录
 */
@Service
public class ZhenMoLuService {
   Logger logger = LoggerFactory.getLogger(ZhenMoLuService.class);

   @Autowired
   PetService petService;
   @Autowired
   Dao dao;
   @Autowired
   EquipService equipService;
   @Autowired
   ServerService serverService;
   @Autowired
   BagService bagService;
   @Autowired
   RoleService roleService;

   boolean isClosed = false;

   public void zhenmoStart(Role role, int total, byte level) {
      ZhenMoLu zhenMoLu = getZhenMoLu(role);
      //检查次数是否已经超限
      if(zhenMoLu.getZmTimes()>zhenMoLu.getMaxZmTimes()){
         MessagePusher.pushMessage(role, new RespNotifyMiscEx("今日次数已满"));
         return;
      }

      //消耗镇魔令

      if(zhenMoLu.getZmlCount()<total){
         MessagePusher.pushMessage(role, new RespNotifyMiscEx("镇魔令不足"));
         return;
      }

      zhenMoLu.setZmlCount((short)(zhenMoLu.getZmlCount() -  total));
      zhenMoLu.setZmTimes((byte) (zhenMoLu.getZmTimes()+1));
      role.getExtendBox().setZhenMoLu(zhenMoLu);
      role.save();

//        MessagePusher.pushMessage(role, new RespNotifyMiscEx("使用#R"+ total +"#n枚#R镇魔令#n"));

      //MSG_ZHENMOLU_DATA               = 45919;    //返回镇魔录数据
      RespZhenMoLuData respZhenMoLuData = new RespZhenMoLuData();
      respZhenMoLuData.setZmlCount(zhenMoLu.getZmlCount());
      respZhenMoLuData.setZmTimes(zhenMoLu.getZmTimes());
      respZhenMoLuData.setMaxZmTimes((byte) 80);
      MessagePusher.pushMessage(role, respZhenMoLuData);



      //开始取随机
      int index = ThreadLocalRandom.current().nextInt(100);

      byte ymType = 0;
      //30%的几率是小妖
      if(index <= 30){
         ymType =1;
      }
      else if(index >= 80){
         ymType = 2;
      }

      role.setZmLevel(level);
      role.setYmType(ymType);
//        //结果 MSG_ZHENMOLU_RESULT             = 45914;
      RespZhenMoLuResult respZhenMoLuResult = new RespZhenMoLuResult();
      respZhenMoLuResult.setFlag((byte) 1);
      respZhenMoLuResult.setFmLevel((byte) level);
      respZhenMoLuResult.setYmType(ymType);
      respZhenMoLuResult.setItemRate((byte) 1);
      MessagePusher.pushMessage(role, respZhenMoLuResult);
//
//        //MSG_ZHENMOLU_BONUS              = 45915;
//        RespZhenMoLuBonus respZhenMoLuBonus = new RespZhenMoLuBonus();
//        respZhenMoLuBonus.setItemName("");
//        respZhenMoLuBonus.setCount((byte) 0);
//        respZhenMoLuBonus.setItemRate((byte) 0);

   }

   /**
    * 打开镇魔录界面
    */
   public void openZmlDlg(Role role, String msg, NPC npc) {
      if (isClosed) {
         MessagePusher.pushMessage(role, new RespMsg("镇魔录维护中..."));
         return;
      }

      ZhenMoLu zhenMoLu = getZhenMoLu(role);
      //发送镇魔录界面数据
      RespZhenMoLuData respZhenMoLuData = new RespZhenMoLuData();
      respZhenMoLuData.setZmlCount(zhenMoLu.getZmlCount());
      respZhenMoLuData.setZmTimes(zhenMoLu.getZmTimes());
      respZhenMoLuData.setMaxZmTimes(zhenMoLu.getMaxZmTimes());
      MessagePusher.pushMessage(role, respZhenMoLuData);

      //打开窗口
      RespGeneralNotify respGeneralNotify = new RespGeneralNotify();
      respGeneralNotify.setNotify(NotifyModules.NOTIFY_OPEN_DLG);
      respGeneralNotify.setValue("ZhenMoLuHUDDlg");
      MessagePusher.pushMessage(role, respGeneralNotify);

   }

   /**
    * 打开净化内丹界面
    * @param role
    * @param msg
    * @param npc
    */
   public void openZmlJinghuaDlg(Role role, String msg, NPC npc) {
      if (isClosed) {
         MessagePusher.pushMessage(role, new RespMsg("镇魔录维护中..."));
         return;
      }

      ZhenMoLu zhenMoLu = getZhenMoLu(role);
      //发送镇魔录界面数据
      RespZhenMoLuData respZhenMoLuData = new RespZhenMoLuData();
      respZhenMoLuData.setZmlCount(zhenMoLu.getZmlCount());
      respZhenMoLuData.setZmTimes(zhenMoLu.getZmTimes());
      respZhenMoLuData.setMaxZmTimes(zhenMoLu.getMaxZmTimes());
      MessagePusher.pushMessage(role, respZhenMoLuData);

      //打开窗口
      RespGeneralNotify respGeneralNotify = new RespGeneralNotify();
      respGeneralNotify.setNotify(NotifyModules.NOTIFY_OPEN_DLG);
      respGeneralNotify.setValue("ZhenMoluChangeBackDlg");
      MessagePusher.pushMessage(role, respGeneralNotify);

   }

   public void buyZML(Role role, int count) {

      int price = 3000000;//300w文

      int total = price* count;
      if(role.getMoney()< total){
         MessagePusher.pushMessage(role, new RespNotifyMiscEx("金钱不足"));
         return;
      }
      //扣钱
      SpringUtils.getRoleService().subtractMoney(role, total);
      MessagePusher.pushMessage(role, new RespNotifyMiscEx("花费#R"+ total +"#n文钱"));

      ZhenMoLu zhenMoLu = getZhenMoLu(role);
      zhenMoLu.setZmlCount((short)(role.getExtendBox().getZhenMoLu().getZmlCount() +  count));
      role.getExtendBox().setZhenMoLu(zhenMoLu);
      role.save();

      //        MSG_UPDATE = 65527;刷新角色面板

      MessagePusher.pushMessage(role, new RespNotifyMiscEx("你花费#R"+total+"文钱#n，成功兑换了"+ count+"枚#R镇魔令#n"));



      RespZhenMoLuData respZhenMoLuData = new RespZhenMoLuData();
      respZhenMoLuData.setZmlCount(zhenMoLu.getZmlCount());
      respZhenMoLuData.setZmTimes(zhenMoLu.getZmTimes());
      respZhenMoLuData.setMaxZmTimes((byte) 80);
      MessagePusher.pushMessage(role, respZhenMoLuData);

      //关闭对话框
      RespGeneralNotify respGeneralNotify = new RespGeneralNotify();
      respGeneralNotify.setNotify(NotifyModules.NOTIFY_CLOSE_DLG);
      respGeneralNotify.setValue("ZhenMoluChangeDlg");
      MessagePusher.pushMessage(role, respGeneralNotify);

   }

   public ZhenMoLu getZhenMoLu(Role role) {
      ZhenMoLu zhenMoLu = role.getExtendBox().getZhenMoLu();
      if (zhenMoLu == null) {
         //数据初始化
         zhenMoLu = new ZhenMoLu();
         role.getExtendBox().setZhenMoLu(zhenMoLu);
         zhenMoLu.setZmTimes((byte) 0);
         zhenMoLu.setZmlCount((byte) 0);
         zhenMoLu.setMaxZmTimes((byte) 80);
         role.save();
      }

      return zhenMoLu;
   }

   /**
    * 早上重置镇魔次数
    * @param role
    */
   public void resetZM(Role role){
      ZhenMoLu zhenMoLu = getZhenMoLu(role);
      zhenMoLu.setZmTimes((byte) 0);
      role.getExtendBox().setZhenMoLu(zhenMoLu);
      role.save();
   }

}
